//
//  CRInputEmoticonButton.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/8/4.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "CRInputEmoticonButton.h"
#import "UIImage+CRKit.h"
#import "CREmoticonManager.h"

@implementation CRInputEmoticonButton

+ (CRInputEmoticonButton*)iconButtonWithData:(CRInputEmoticon*)data
                                   catalogID:(NSString*)catalogID
                                    delegate:( id<CREmoticonButtonTouchDelegate>)delegate {
    
    CRInputEmoticonButton* icon = [[CRInputEmoticonButton alloc] init];
    [icon addTarget:icon action:@selector(onIconSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    icon.emoticonData    = data;
    icon.catalogID              = catalogID;
    icon.userInteractionEnabled = YES;
    icon.exclusiveTouch         = YES;
    icon.contentMode            = UIViewContentModeScaleToFill;
    icon.delegate               = delegate;
    
    switch (data.type) {
        case CREmoticonTypeUnicode:
        {
            [icon setTitle:data.unicode forState:UIControlStateNormal];
            [icon setTitle:data.unicode forState:UIControlStateHighlighted];
            icon.titleLabel.font = [UIFont systemFontOfSize:32];
            break;
        }
        case CREmoticonTypeFile:
        default:
        {
            UIImage *image = [UIImage cr_emoticonInKit:data.filename];
            [icon setImage:image forState:UIControlStateNormal];
            [icon setImage:image forState:UIControlStateHighlighted];
            break;
        }
    }
    return icon;
}

- (void)onIconSelected:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(selectedEmoticon:catalogID:)]) {
        [self.delegate selectedEmoticon:self.emoticonData catalogID:self.catalogID];
    }
}

@end
